import request from "@/plugins/request";
import requestDic from "@/plugins/request/request_dic";
import requestFile from "@/plugins/request/request_file";
import Setting from "../setting";

// 获取表格数据
export function getTabData(url, data, method = "post") {
  return request({ url: `${url}`, data, method });
}
export function getTabDataSupport(url, data, method = "get") {
  return requestDic({ url: `${url}`, data, method });
}

// 查询当前序列值
export function queryCurrentValue(seqCode) {
  return requestDic({
    url: `/support/seq/queryCurrentValue?seqCode=${seqCode}`,
    method: "get",
  });
}

// 查询数据字典值
export function queryDic(dicValue) {
  return requestDic({
    url: `/support/dic/code/component/list?dicValue=${dicValue}`,
    method: "get",
  });
}

// 查询行政区划
export function queryArea(data, type) {
  return requestDic({
    url: `/support/area/queryByPAreaCode?pAreaCode=${data}&areaType=${type}`,
    method: "get",
  });
}

// 文件上传
export function uploadAttach(data) {
  let _formData = new FormData();
  _formData.append("file", data);
  return requestFile({
    url: `/file/upload`,
    method: "post",
    headers: { "Content-Type": "multipart/form-data" },
    data: _formData,
  });
}

// 获取文件访问url
export async function getFileInfo(params) {
  if (Setting.codeMode === "uapRelease") {
    let res = await requestFile({
      url: `/file/url`,
      method: "get",
      params,
    });
    if (res.code === 1000 && res.data) {
      res.data = changeUrlPre(res.data);
    }
    return new Promise(function (resolve) {
      resolve(res);
    });
  } else {
    return requestFile({
      url: `/file/url`,
      method: "get",
      params,
    });
  }
}

// 获取文件访问url及描述信息
export async function queryFileInfo(data) {
  if (Setting.codeMode === "uapRelease") {
    let res = await requestFile({
      url: `/file/url/info`,
      method: "post",
      data,
    });
    if (res.code === 1000 && res.data) {
      res.data.url = changeUrlPre(res.data.url);
      res.data.previewUrl = changeUrlPre(res.data.previewUrl);
    }
    return new Promise(function (resolve) {
      resolve(res);
    });
  } else {
    return requestFile({
      url: `/file/url/info`,
      method: "post",
      data,
    });
  }
}

// 获取多文件访问url及描述信息
export async function queryFileInfoList(data) {
  if (Setting.codeMode === "uapRelease") {
    let res = await requestFile({
      url: `/file/url/info/list`,
      method: "post",
      data,
    });
    if (res.code === 1000 && res.data) {
      res.data.forEach((item) => {
        item.url = changeUrlPre(item.url);
        item.previewUrl = changeUrlPre(res.data.previewUrl);
      });
    }
    return new Promise(function (resolve) {
      resolve(res);
    });
  } else {
    return requestFile({
      url: `/file/url/info/list`,
      method: "post",
      data,
    });
  }
}

// 获取多文件访问url及描述信息
export async function queryFileList(data) {
  if (Setting.codeMode === "uapRelease") {
    let res = await requestFile({
      url: `/file/info/list`,
      method: "post",
      data,
    });
    if (res.code === 1000 && res.data) {
      res.data.forEach((item) => {
        item.url = changeUrlPre(item.url);
        item.previewUrl = changeUrlPre(item.previewUrl);
      });
    }
    return new Promise(function (resolve) {
      resolve(res);
    });
  } else {
    return requestFile({
      url: `/file/info/list`,
      method: "post",
      data,
    });
  }
}

// 下载文件
export function downFileForM(url) {
  return requestFile({
    url: `/file/inline/${url}`,
    method: "get",
    responseType: "blob",
  });
}

// 查询单个系统参数配置值
export function getSystemPatamValue(params) {
  let prefix = "";
  if (process.env.NODE_ENV !== "development") {
    prefix = "/api/ps/framework";
  }
  return request({
    url: `${prefix}/support/config/queryConfigValue`,
    method: "get",
    params,
  });
}

// uap环境附件预览地址前缀替换
function changeUrlPre(url) {
  if (!url) return url;
  let str = url.split("/").slice(0, 3).join("/");
  let newPre = Setting.fileUrlPre + sessionStorage.getItem("UAP_ISC_PORT");
  return url.replace(str, newPre);
}
